
public class Controller {
	
	final static int NUMBER_PACKETS_PER_BATCH = 200;
	final static int NUMBER_OF_RUNS = 500;
	
//	final static int NUMBER_PACKETS_PER_BATCH = 100000;
//	final static int NUMBER_OF_RUNS = 1;

	public static void main(String[] argsv){
		
		
		int bsize;
		
		String data = "abcdefghijklm";
		//String data = "abcdefghijklmabcdefghijklmabcdefghijklmabcdefghi";
		//String data = "abcdefghijklmabcdefghijklmabcdefghijklmabcdefghiabcdefghijklmabcdefghijklmabcdefghijklmabcdefghi";
		bsize = data.length() + 5;
				
		if(argsv.length < 1){
			System.out.println("Enter either an argument of 1 to run a TCP test or 2 to run a UDP test");
			System.exit(0);
		}
						
		for(int j = 0; j < NUMBER_OF_RUNS; j++){
			
			switch(Integer.parseInt(argsv[0])){
			case 1:
				tcpStarter(data);
				break;
			case 2:
				udpStarter(data, bsize);
				break;
			default:
				System.out.println("Enter either an argument of 1 to run a TCP test or 2 to run a UDP test");
			}
					
			System.out.println(j + "-------------------------");
		}

		System.exit(0);
	}
	
	public static void udpStarter(String data, int bsize){
		//create a UDPTalker & UDPListener pair
		Thread udpt = new Thread(new UDPTalker(6000, "127.0.0.1", NUMBER_PACKETS_PER_BATCH, data, bsize));
		Thread udpl = new Thread(new UDPListener(6000, bsize));
		
		udpl.start();
		udpt.start();
		
		//wait for tcpt & tcpl to die
		try {	
			udpt.join();
			udpl.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		udpt = null;
		udpl = null;
		
	}
	
	public static void tcpStarter(String data){
		//create a TCPTalker & TCPListener pair
		Thread tcpt = new Thread(new TCPTalker(5001, "127.0.0.1", NUMBER_PACKETS_PER_BATCH, data));
		Thread tcpl = new Thread(new TCPListener(5001));
		
		tcpl.start();
		tcpt.start();
		
		//wait for tcpt & tcpl to die
		try {	
			tcpt.join();
			tcpl.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//null time
		tcpt = null;
		tcpl = null;
	}
}
